import pageUtils from '../utils/page_utils.js'
const createMixin = mixin => {
    return config => {
        const result = { ...config };
        Object.keys(mixin).forEach(key => {
            const originalData = config[key], mixinData = mixin[key];
            result[key] = async function (...args) {
                try {
                    let res;
                    res = await mixinData.apply(this, args);
                    if (originalData) {
                        // 调用原始函数并返回结果（如果存在）
                        res = await originalData.apply(this, args)
                    }
                    return res
                } catch (error) {
                    console.error(`Error in mixin function '${key}':`, error);
                    // 错误重新抛出 或者 return null;
                    throw error;
                }
            };
        });

        return result;
    };
}
const Custom_Page = createMixin({
    // 弹窗显隐控制banner
    isShowBannerHandle(n, o) {
        if (n) {
            // 移除横幅
            AD_SDK.hideBannerAdv()
        }
    },
    async onInit() {
        if (!this.pageDetail?.pageCode || this.pageDetail.pageCode == 'DESK_ROOT') return
        console.info(`生命周期-onInit`, this.pageDetail.pageName)
        await pageUtils.pageInit(this)
    },
    async onShow() {
        if (!this.pageDetail?.pageCode || this.pageDetail.pageCode == 'DESK_ROOT') return
        console.info(`生命周期-页面：onShow`, this.pageDetail.pageName)
        await pageUtils.pageShow(this)
    },
    async onHide() {
        if (!this.pageDetail?.pageCode || this.pageDetail.pageCode == 'DESK_ROOT') return
        console.info(`生命周期-页面：onShow`, this.pageDetail.pageName)
        await pageUtils.pageHide(this)
    },
    /**
     * 接受自定义通用组件控制器派发的数据
    */
    nativeBoostGroupEventDispatchHandler(evt) {
        LOG('VIEW', `nativeBoostGroupEventDispatchHandler======>`, evt)
        let _this = this
        switch (evt.detail.eventName) {
            case 'noBoostComp4Use': //没有可使用的补量组件
                let _cudPopCode = _this.nativeBoostGroupController.popCode
                let _cudActionCode = _this.nativeBoostGroupController.actionCode
                LOG('noBoostComp4Use-boostGroupEvent')
                POP_TOOLS.noAd4UseEventHandler({ actionCode: _cudActionCode, code: _cudPopCode }, { ..._this.pageDetail }, _this.$app.$def)
                break
            case 'noPopCode':
                LOG('noPopCode-boostGroupEvent')
                POP_TOOLS.commonFunc2PatchPopShow(_this.pageDetail, "PAGE_BACK", '', _this.pageDetail.pageRoute, _this.$app.$def)
                break
            case 'firstAdLoadComplete':
                LOG('firstAdLoadComplete-boostGroupEvent')
                POP_TOOLS.commonFunc2PatchPopShow(_this.pageDetail, "PAGE_ENTRY", '', _this.pageDetail.pageRoute, _this.$app.$def)
                break
            case 'closePopData':
                _this.nativeBoostGroupController.popShow = false
                _this.nativeBoostGroupController.popCode = ''
                POP_TOOLS.commonPopControllerEventDispatch(evt.detail, { ..._this.pageDetail }, _this.$app.$def)
                break
            case 'popShow':
                _this.nativeBoostGroupController.popShow = true
                break
        }
    },
    /**
     * 接受自定义通用视频和百度组件控制器派发的数据
     */
    bqtVideoAdControllerEventDispatchHandler(evt) {
        LOG('BQT_AD', `bqtVideoAdControllerEventDispatchHandler======>`, evt)
        let _this = this
        switch (evt.detail.eventName) {
            case 'closePopData':
                POP_TOOLS.bqtVideoAdEventDispatch(evt.detail, { ..._this.pageDetail }, _this.$app.$def)
                if (evt.detail.action !== 'videoAdClick') {
                    _this.videoBqtConfig.isShow = false
                }
                break
        }
    },
    /**
    * banner广告事件回调 -event
    */
    adBannerEventWatchHandler(evt) {
        let _this = this
        switch (evt.detail.eventName) {
            case 'adClickImg':
                AD_SDK_UTILS.clickManuAdvForImg(_this.bannerObj, _this.pageDetail.pageName)
                _this.bannerObj = null
                break
            case 'closeAdClick':
                COMMON_REPORT_UTILS.page_click_report(`关闭广告`, '', ``, ``) //点击上报    
                _this.bannerObj = null
                break
        }
    },
    // 打开白青藤或视频自定义弹窗
    addBqtVideoPop(popCode = "Baidu_KHEKCERA", actionCode = "PAGE_ENTRY", customAction = {}) {
        let _this = this
        if (_this.videoBqtConfig.isShow) {
            _this.videoBqtConfig.isShow = false
        }
        _this.videoBqtConfig.popCode = popCode
        _this.videoBqtConfig.actionCode = actionCode
        _this.videoBqtConfig.customAction = customAction
        _this.videoBqtConfig.isShow = true
    },
    onDestroy() {
        AD_SDK.destroyBannerAdv()
    },
})
export default Custom_Page
